// pages/order/logis/logistical.js
const app = getApp()
var util = require('../../../utils/util.js')
var http = require('../../../utils/server.js')
Page({

  data: {

  },

  onLoad: function(options) {
    var that = this;
    options.index = options.index || 0
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面（父页面）

    var order = prevPage.data.data;
    if (!order && options.order_id) {
      http.post({
        url: "order/getinfo",
        order_id: options.order_id
      }, function(data) {
        console.info(data);
        order = data
        that.setData({
          order: order
        })
        that.getData(options.index);
      })
    } else {
      that.setData({
        order: order
      });
      that.getData(options.index);
    }
  },
  getData: function(index) {
    var that = this;
    http.post({
      url: "Address/getAllExpress",
      type: ''
    }, function(data) {
      var order = that.data.order
      for (var i = 0; i < data.length; i++) {
        for (var add in order.address) {
          var address = order.address[add]
          if (address.express_code == data[i].code) {
            address.name = data[i].name
          }
        }
      }
      var datas = new Array(order.address.length);
      that.setData({
        order: order,
        datas: datas
      });
      that.load(index)
    })
  },
  load: function(index) {
    var that = this;
    if (that.data.datas[index] != null) {
      that.setData({
        data: that.data.datas[index]
      });
      return;
    }
    var order = that.data.order;
    var add = order.address[index];
    http.post({
      url: "order/getLogistics",
      code: add.express_code,
      num: add.express_number,
    }, function(data) {
      that.data.datas[index] = data.data;
      that.setData({
        data: data.data
      });
    })
  },
  change: function(e) {
    console.info(e);
    var index = e.detail.current;
    this.load(index)
  },
})